package com.bigknow.appstore.common.util;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

/**
 * JDBC操作相关帮助方法
 */
public class SQLUtil {

    private SQLUtil() {
    }

    public static final void close(Connection cn) {
        close(null, null, cn);
    }

    public static final void close(Statement stmt) {
        close(null, stmt, null);
    }

    public static final void close(Statement stmt, Connection cn) {
        close(null, stmt, cn);
    }

    public static final void close(ResultSet rs) {
        close(rs, null, null);
    }

    public static final void close(ResultSet rs, Statement stmt) {
        close(rs, stmt, null);
    }

    public static final void close(ResultSet rs, Connection cn) {
        close(rs, null, cn);
    }

    public static void close(ResultSet rs, Statement stmt, Connection cn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
            }
            rs = null;
        }

        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
            }
            stmt = null;
        }

        if (cn != null) {
            try {
                cn.close();
            } catch (Exception e) {
            }
            cn = null;
        }
    }

    public static void executeSql(Connection conn, String sql, Object... args) throws SQLException {
        PreparedStatement st = null;
        try {
            st = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                st.setObject(i + 1, args[i]);
            }
            st.execute();
        } finally {
            close(st, conn);
        }
    }

    public static String getSDOTypeBySQLType(int type) {
        String rType = null;
        switch (type) {
            case Types.BIGINT:
                rType = "long";
                break;
            case Types.CHAR:
            case Types.VARCHAR:
            case Types.LONGVARCHAR:
            case Types.CLOB:
            case -103: // 针对Oracle INTERVALDS----104
            case -104: // 针对Oracle INTERVALYM----103
                rType = "string";
                break;
            case Types.NUMERIC:
            case Types.DECIMAL:
            case Types.DOUBLE:
            case Types.REAL:
            case 100: // 针对Oracle的BINARY_DOUBLE
                rType = "double";
                break;
            case Types.BIT:
            case Types.BOOLEAN:
                rType = "boolean";
                break;
            case Types.TINYINT:
            case Types.SMALLINT:
                rType = "short";
                break;
            case Types.INTEGER:
                rType = "int";
                break;
            case Types.FLOAT:
            case 101: // 针对Oracle的BINARY_FLOAT
                rType = "float";
                break;
            case Types.BINARY:
            case Types.VARBINARY:
            case Types.LONGVARBINARY:
            case Types.BLOB:
            case Types.STRUCT:
                rType = "bytes";
                break;
            case Types.DATE:
                rType = "date";
                break;
            case Types.TIME:
                rType = "time";
                break;
            case Types.TIMESTAMP:
                rType = "datetime";
                break;
            case Types.DISTINCT:
            case Types.REF:
            case Types.DATALINK:
            case Types.JAVA_OBJECT:
                rType = "object";
                break;
            default:
                rType = "object";
        }
        return rType;
    }

    private static Map<String, Object> simpleValues = new HashMap<>();

    static {
        simpleValues.put("string", "string");
        simpleValues.put("object", "string");
        simpleValues.put("long", 0l);
        simpleValues.put("double", 0.0d);
        simpleValues.put("float", 0.0f);
        simpleValues.put("int", 0);
        simpleValues.put("short", (short) 0);
        simpleValues.put("boolean", true);
        simpleValues.put("bytes", "dGVzdA==");
        simpleValues.put("datetime", "2000-01-01 00:00:00.000");
        simpleValues.put("date", "2000-01-01");
        simpleValues.put("time", "00:00:00.000");
    }

    public static Object getSDOTypeSimpleValue(String type) {
        return simpleValues.get(type);
    }
}
